
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>Java 基础 -- 深入理解 final 修饰符 · 寒江蓑笠的博客</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="介绍 final 修饰符">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="java-basic-memory-manage-constructor.html" />
    
    
    <link rel="prev" href="java-basic-io-stream.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    Java
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1.1" data-path="java-basic-joor.html">
            
                <a href="java-basic-joor.html">
            
                    
                    Java 基础 -- 使用 JOOR 实现反射
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.2" data-path="java-basic-annotation.html">
            
                <a href="java-basic-annotation.html">
            
                    
                    Java基础 -- 注解
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.3" data-path="java-basic-threadlocal.html">
            
                <a href="java-basic-threadlocal.html">
            
                    
                    Java 基础 -- ThreadLocal
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.4" data-path="java-development-skills-get-stacktrace.html">
            
                <a href="java-development-skills-get-stacktrace.html">
            
                    
                    Java 开发技巧 -- 获取 Java 的调用栈
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.5" data-path="java-basic-concurrent-map-collection.html">
            
                <a href="java-basic-concurrent-map-collection.html">
            
                    
                    Java 基础 -- 并发集合的使用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.6" data-path="java-dynamic-proxy-invocationhandler-demo.html">
            
                <a href="java-dynamic-proxy-invocationhandler-demo.html">
            
                    
                    Java InvocationHandler 实现动态代理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.7" data-path="java-basic-try-catch-finally.html">
            
                <a href="java-basic-try-catch-finally.html">
            
                    
                    Java 基础 -- 深入理解 try catch finally
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.8" data-path="java-basic-reflect.html">
            
                <a href="java-basic-reflect.html">
            
                    
                    Java 基础 -- 反射
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.9" data-path="java-basic-io-stream.html">
            
                <a href="java-basic-io-stream.html">
            
                    
                    Java IO -- IO流的概念和分类
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.1.10" data-path="java-basic-final.html">
            
                <a href="java-basic-final.html">
            
                    
                    Java 基础 -- 深入理解 final 修饰符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.11" data-path="java-basic-memory-manage-constructor.html">
            
                <a href="java-basic-memory-manage-constructor.html">
            
                    
                    Java 内存管理 -- 父子实例的内存控制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.12" data-path="java-basic-memory-manage-inherit-object.html">
            
                <a href="java-basic-memory-manage-inherit-object.html">
            
                    
                    Java 内存管理 -- 构造方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.13" data-path="java-basic-memory-manage-memory-area.html">
            
                <a href="java-basic-memory-manage-memory-area.html">
            
                    
                    Java 内存管理 -- 内存区域的划分
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.14" data-path="java-basic-memory-manage-variable.html">
            
                <a href="java-basic-memory-manage-variable.html">
            
                    
                    Java 内存管理 -- 变量的内存管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.15" data-path="java-basic-knowledge-generic.html">
            
                <a href="java-basic-knowledge-generic.html">
            
                    
                    Java 基础 -- 范型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.16" data-path="java-basic-knowledge-auto-boxing.html">
            
                <a href="java-basic-knowledge-auto-boxing.html">
            
                    
                    Java 基础 -- 关于自动装箱和拆箱机制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.17" data-path="java-basic-knowledge-parameter-passing.html">
            
                <a href="java-basic-knowledge-parameter-passing.html">
            
                    
                    Java 基础 -- 值传递、引用传递和参数传递
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.18" data-path="java-basic-stringbuilder-stringbuffer.html">
            
                <a href="java-basic-stringbuilder-stringbuffer.html">
            
                    
                    Java 基础 -- StringBuilder 和 StringBuffer
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.19" data-path="java-basic-knowledge-about-string.html">
            
                <a href="java-basic-knowledge-about-string.html">
            
                    
                    Java 基础 -- 深入理解 String
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.20" data-path="java-basic-linkedhashmap.html">
            
                <a href="java-basic-linkedhashmap.html">
            
                    
                    Java 基础 -- LinkedHashMap 的使用和原理
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >Java 基础 -- 深入理解 final 修饰符</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h2 id="&#x6982;&#x8FF0;">&#x6982;&#x8FF0;</h2>
<p>final &#x4FEE;&#x9970;&#x7B26;&#x4E5F;&#x662F;&#x6211;&#x4EEC;&#x7ECF;&#x5E38;&#x7528;&#x5230;&#x7684;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x4FEE;&#x9970;&#x7B26;&#xFF0C;final&#x4FEE;&#x9970;&#x7B26;&#x7684;&#x57FA;&#x672C;&#x4F5C;&#x7528;&#x5982;&#x4E0B;&#xFF1A;</p>
<ul>
<li>final &#x53EF;&#x4EE5;&#x4FEE;&#x9970;&#x53D8;&#x91CF;&#xFF0C;&#x88AB;final&#x4FEE;&#x9970;&#x7684;&#x53D8;&#x91CF;&#x88AB;&#x8D4B;&#x503C;&#x540E;&#xFF0C;&#x4E0D;&#x80FD;&#x5BF9;&#x5B83;&#x91CD;&#x65B0;&#x8D4B;&#x503C;</li>
<li>final &#x53EF;&#x4EE5;&#x4FEE;&#x9970;&#x65B9;&#x6CD5;&#xFF0C;&#x88AB;final&#x4FEE;&#x9970;&#x7684;&#x65B9;&#x6CD5;&#x4E0D;&#x80FD;&#x88AB;&#x91CD;&#x5199;</li>
<li>final &#x53EF;&#x4EE5;&#x4FEE;&#x9970;&#x7C7B;&#xFF0C;&#x88AB;final&#x4FEE;&#x9970;&#x7684;&#x7C7B;&#x4E0D;&#x80FD;&#x6D3E;&#x751F;&#x5B50;&#x7C7B;</li>
</ul>
<p>&#x4F46;&#x662F;&#xFF0C;&#x4EC5;&#x4EC5;&#x4E86;&#x89E3;&#x8FD9;&#x4E9B;&#x5BF9;&#x4E8E;&#x771F;&#x6B63;&#x638C;&#x63E1;final&#x4FEE;&#x9970;&#x7B26;&#x7684;&#x7528;&#x6CD5;&#x662F;&#x8FDC;&#x8FDC;&#x4E0D;&#x591F;&#x7684;&#xFF0C;&#x672C;&#x6587;&#x5C06;&#x5C1D;&#x8BD5;&#x6DF1;&#x5165;&#x5730;&#x4E86;&#x89E3;final&#x4FEE;&#x9970;&#x7B26;&#x3002;</p>
<h2 id="final-&#x4FEE;&#x9970;&#x53D8;&#x91CF;&#x7684;&#x521D;&#x59CB;&#x5316;">final &#x4FEE;&#x9970;&#x53D8;&#x91CF;&#x7684;&#x521D;&#x59CB;&#x5316;</h2>
<h3 id="final-&#x5B9E;&#x4F8B;&#x53D8;&#x91CF;">final &#x5B9E;&#x4F8B;&#x53D8;&#x91CF;</h3>
<p>&#x88AB; final &#x4FEE;&#x9970;&#x7684;&#x5B9E;&#x4F8B;&#x53D8;&#x91CF;&#x5FC5;&#x987B;&#x663E;&#x793A;&#x6307;&#x5B9A;&#x521D;&#x59CB;&#x503C;&#xFF0C;&#x800C;&#x4E14;&#x53EA;&#x80FD;&#x5728;&#x5982;&#x4E0B;3&#x4E2A;&#x4F4D;&#x7F6E;&#x6307;&#x5B9A;&#x521D;&#x59CB;&#x503C;&#x3002;</p>
<ul>
<li>&#x5B9A;&#x4E49;final&#x5B9E;&#x4F8B;&#x53D8;&#x91CF;&#x65F6;&#x6307;&#x5B9A;&#x521D;&#x59CB;&#x503C;</li>
<li>&#x5728;&#x975E;&#x9759;&#x6001;&#x521D;&#x59CB;&#x5316;&#x5757;&#x4E2D;&#x4E3A;final&#x5B9E;&#x4F8B;&#x53D8;&#x91CF;&#x6307;&#x5B9A;&#x521D;&#x59CB;&#x503C;</li>
<li>&#x5728;&#x6784;&#x9020;&#x5668;&#x4E2D;&#x4E3A;final&#x5B9E;&#x4F8B;&#x53D8;&#x91CF;&#x6307;&#x5B9A;&#x521D;&#x59CB;&#x503C;</li>
</ul>
<p>&#x5BF9;&#x4E8E;&#x666E;&#x901A;&#x7684;&#x5B9E;&#x4F8B;&#x53D8;&#x91CF;&#xFF0C;Java&#x7A0B;&#x5E8F;&#x53EF;&#x4EE5;&#x5BF9;&#x5B83;&#x6267;&#x884C;&#x9ED8;&#x8BA4;&#x7684;&#x521D;&#x59CB;&#x5316;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x5728;&#x521D;&#x59CB;&#x7C7B;&#x65F6;JVM&#x4F1A;&#x5C06;&#x5B9E;&#x4F8B;&#x53D8;&#x91CF;&#x7684;&#x503C;&#x6307;&#x5B9A;&#x4E3A;&#x9ED8;&#x8BA4;&#x7684;&#x521D;&#x59CB;&#x503C;&#x3002;&#x4F46;&#x5BF9;&#x4E8E;final&#x5B9E;&#x4F8B;&#x53D8;&#x91CF;&#xFF0C;&#x5219;&#x5FC5;&#x987B;&#x6709;&#x7A0B;&#x5E8F;&#x5458;&#x663E;&#x793A;&#x5730;&#x6307;&#x5B9A;&#x521D;&#x59CB;&#x503C;&#x3002;</p>
<p><strong>&#x6E29;&#x99A8;&#x63D0;&#x793A;&#xFF1A;</strong>&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x4E5F;&#x5FC5;&#x987B;&#x663E;&#x793A;&#x5730;&#x521D;&#x59CB;&#x5316;&#x3002;</p>
<p>&#x770B;&#x4EE3;&#x7801;&#x5B9E;&#x73B0;&#xFF1A;</p>
<pre><code>public class Test {
    // &#x5B9A;&#x4E49;final&#x5B9E;&#x4F8B;&#x53D8;&#x91CF;&#x65F6;&#x8D4B;&#x521D;&#x59CB;&#x503C;
    public final String str1 = &quot;str1&quot;;
    public final String str2;
    public final String str3;

    {
        // &#x5728;&#x521D;&#x59CB;&#x5316;&#x5757;&#x4E2D;&#x8D4B;&#x521D;&#x59CB;&#x503C;
        str2 = &quot;str2&quot;;
    }

    public Test(){
        // &#x5728;&#x6784;&#x9020;&#x65B9;&#x6CD5;&#x4E2D;&#x8D4B;&#x521D;&#x59CB;&#x503C;
        str3 = &quot;str3&quot;;
    }
}
</code></pre><p>&#x8FD9;&#x91CC;&#x9700;&#x8981;&#x6307;&#x51FA;&#x7684;&#x662F;&#xFF0C;&#x5176;&#x4ED6;&#x7ECF;&#x8FC7;&#x7F16;&#x8BD1;&#x5668;&#x7684;&#x5904;&#x7406;&#xFF0C;&#x8FD9;3&#x4E2D;&#x65B9;&#x5F0F;&#x90FD;&#x4F1A;&#x88AB;&#x62BD;&#x53D6;&#x5230;&#x6784;&#x9020;&#x65B9;&#x6CD5;&#x4E2D;&#x8FDB;&#x884C;&#x8D4B;&#x521D;&#x59CB;&#x503C;&#x64CD;&#x4F5C;&#x3002;
&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; javap &#x547D;&#x4EE4;&#x5DE5;&#x5177;&#x7C7B;&#x5206;&#x6790;&#x4E00;&#x4E0B;&#x8BE5;&#x4EE3;&#x7801;&#xFF1A;
<code>javap -c Test.calss</code></p>
<pre><code>Compiled from &quot;Test.java&quot;
public class com.example.heqiang.testsomething.commontest.Test {
  public final java.lang.String str1;

  public final java.lang.String str2;

  public final java.lang.String str3;
  // &#x6784;&#x9020;&#x65B9;&#x6CD5;&#x4EE3;&#x7801;
  public com.example.heqiang.testsomething.commontest.Test();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object.&quot;&lt;init&gt;&quot;:()V
       4: aload_0
       5: ldc           #2                  // String str1
       7: putfield      #3                  // Field str1:Ljava/lang/String;
      10: aload_0
      11: ldc           #4                  // String str2
      13: putfield      #5                  // Field str2:Ljava/lang/String;
      16: aload_0
      17: ldc           #6                  // String str3
      19: putfield      #7                  // Field str3:Ljava/lang/String;
      22: return
}
</code></pre><h3 id="final-&#x7C7B;&#x53D8;&#x91CF;&#xFF08;&#x9759;&#x6001;&#x53D8;&#x91CF;&#xFF09;">final &#x7C7B;&#x53D8;&#x91CF;&#xFF08;&#x9759;&#x6001;&#x53D8;&#x91CF;&#xFF09;</h3>
<p>&#x5BF9;&#x4E8E;&#x7C7B;&#x53D8;&#x91CF;&#x4E5F;&#x5C31;&#x662F;&#x9759;&#x6001;&#x53D8;&#x91CF;&#x800C;&#x8A00;&#xFF0C;&#x540C;&#x6837;&#x4E5F;&#x5FC5;&#x987B;&#x663E;&#x5F0F;&#x6307;&#x5B9A;&#x521D;&#x59CB;&#x503C;&#xFF0C;&#x4E0D;&#x540C;&#x7684;&#x662F;final&#x9759;&#x6001;&#x53D8;&#x91CF;&#x53EA;&#x80FD;&#x5728;&#x4E24;&#x4E2A;&#x5730;&#x65B9;&#x6307;&#x5B9A;&#x521D;&#x59CB;&#x503C;&#xFF1A;</p>
<ul>
<li>&#x5B9A;&#x4E49;final&#x9759;&#x6001;&#x53D8;&#x91CF;&#x65F6;&#x6307;&#x5B9A;&#x521D;&#x59CB;&#x503C;</li>
<li>&#x5728;&#x9759;&#x6001;&#x521D;&#x59CB;&#x5316;&#x4EE3;&#x7801;&#x5757;&#x4E2D;&#x4E3A;final&#x7C7B;&#x53D8;&#x91CF;&#x6307;&#x5B9A;&#x521D;&#x59CB;&#x503C;</li>
</ul>
<pre><code>public class Test {
    // &#x5B9A;&#x4E49;final&#x9759;&#x6001;&#x53D8;&#x91CF;&#x65F6;&#x8D4B;&#x521D;&#x59CB;&#x503C;
    public final static String str1 = &quot;str1&quot;;
    public final static String str2;

    static {
        // &#x5728;&#x9759;&#x6001;&#x521D;&#x59CB;&#x5316;&#x5757;&#x4E2D;&#x8D4B;&#x521D;&#x59CB;&#x503C;
        str2 = &quot;str2&quot;;
    }

    public Test(){
    }
}
</code></pre><h3 id="final-&#x5C40;&#x90E8;&#x53D8;&#x91CF;">final &#x5C40;&#x90E8;&#x53D8;&#x91CF;</h3>
<p>Java &#x672C;&#x6765;&#x5C31;&#x8981;&#x6C42;&#x666E;&#x901A;&#x7684;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x5FC5;&#x987B;&#x663E;&#x5F0F;&#x5730;&#x8D4B;&#x521D;&#x59CB;&#x503C;&#xFF0C;final &#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x4E00;&#x6837;&#x4E5F;&#x9700;&#x8981;&#x663E;&#x5F0F;&#x5730;&#x8D4B;&#x521D;&#x59CB;&#x503C;&#x3002;&#x4E0E;&#x666E;&#x901A;&#x53D8;&#x91CF;&#x4E0D;&#x540C;&#x7684;&#x662F;&#xFF1A;final &#x88AB;&#x8D4B;&#x521D;&#x59CB;&#x503C;&#x540E;&#xFF0C;&#x518D;&#x4E5F;&#x4E0D;&#x80FD;&#x5BF9;final&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x8FDB;&#x884C;&#x8D4B;&#x503C;&#x3002;</p>
<h2 id="&#x6267;&#x884C;-&#x5B8F;&#x66FF;&#x6362;">&#x6267;&#x884C; &#x201C;&#x5B8F;&#x66FF;&#x6362;&#x201D;</h2>
<p>&#x5BF9;&#x4E8E;&#x4E00;&#x4E2A;final&#x53D8;&#x91CF;&#x800C;&#x8A00;&#xFF0C;&#x5982;&#x679C;&#x5728;&#x5B9A;&#x4E49;final&#x53D8;&#x91CF;&#x65F6;&#x6307;&#x5B9A;&#x4E86;&#x521D;&#x59CB;&#x503C;&#xFF0C;&#x800C;&#x4E14;&#x8BE5;&#x521D;&#x59CB;&#x503C;&#x53EF;&#x4EE5;&#x5728;&#x7F16;&#x8BD1;&#x65F6;&#x88AB;&#x786E;&#x5B9A;&#x4E0B;&#x6765;&#xFF0C;&#x90A3;&#x4E48;&#x8FD9;&#x4E2A;final&#x53D8;&#x91CF;&#x672C;&#x8D28;&#x5DF2;&#x7ECF;&#x4E0D;&#x518D;&#x662F;&#x4E00;&#x4E2A;&#x53D8;&#x91CF;&#xFF0C;&#x800C;&#x662F;&#x4E00;&#x4E2A;&#x76F4;&#x63A5;&#x91CF;&#xFF0C;&#x7F16;&#x8BD1;&#x5668;&#x4F1A;&#x5728;&#x7F16;&#x8BD1;&#x65F6;&#x505A;&#x4F18;&#x5316;&#x5904;&#x7406;&#xFF0C;&#x5728;&#x53D8;&#x91CF;&#x51FA;&#x73B0;&#x7684;&#x5730;&#x65B9;&#x76F4;&#x63A5;&#x7528;&#x8BE5;&#x521D;&#x59CB;&#x503C;&#x66FF;&#x6362;&#x53D8;&#x91CF;&#x3002;&#x7C7B;&#x4F3C;&#x5B8F;&#x7684;&#x6548;&#x679C;&#x3002;
final &#x4FEE;&#x9970;&#x7B26;&#x7684;&#x4E00;&#x4E2A;&#x91CD;&#x8981;&#x7528;&#x9014;&#x5C31;&#x662F;&#x5B9A;&#x4E49;&#x201C;&#x5B8F;&#x53D8;&#x91CF;&#x201D;&#x3002;&#x4F46;&#x662F;&#x9700;&#x8981;&#x6709;&#x4E24;&#x4E2A;&#x6761;&#x4EF6;&#xFF1A;</p>
<ul>
<li>&#x5728;&#x5B9A;&#x4E49;final&#x53D8;&#x91CF;&#x65F6;&#x6307;&#x5B9A;&#x4E86;&#x521D;&#x59CB;&#x503C;</li>
<li>&#x8BE5;&#x521D;&#x59CB;&#x503C;&#x53EF;&#x5728;&#x7F16;&#x8BD1;&#x65F6;&#x88AB;&#x786E;&#x5B9A;&#x4E0B;&#x6765;</li>
</ul>
<p>&#x4E3E;&#x4F8B;&#xFF1A;</p>
<pre><code>public class Test {
    final String str1 = &quot;str1&quot;;

    public String getString(){
        return str1;
    }
}
</code></pre><p>&#x8FD9;&#x6BB5;&#x4EE3;&#x7801;&#x5176;&#x5B9E;&#x5DF2;&#x7ECF;&#x88AB;&#x7F16;&#x8BD1;&#x5668;&#x4F18;&#x5316;&#x6210;&#x4E86;&#xFF1A;</p>
<pre><code>public class Test {
    public String getString(){
        return &quot;str1&quot;;
    }
}
</code></pre><p>&#x6211;&#x4EEC;&#x5199;&#x4E24;&#x4E2A;&#x4F8B;&#x5B50;&#x9A8C;&#x8BC1;&#x4E00;&#x4E0B;&#x8FD9;&#x4E2A;&#x7ED3;&#x8BBA;&#xFF1A;</p>
<pre><code>public class Price {
    final static Price INSTANCE = new Price(2.8);
    static double initPrice = 20;

    double currentPrice;
    public Price(double discount) {
        currentPrice = initPrice - discount;
    }
}
</code></pre><p>&#x6D4B;&#x8BD5;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code>Log.e(&quot;Test&quot;,&quot;&quot; + Price.INSTANCE.currentPrice);
</code></pre><p>&#x8FD9;&#x91CC;&#x8F93;&#x51FA;&#xFF1A;-2.8
&#x539F;&#x7406;&#x5728;&#x524D;&#x9762;&#x6587;&#x7AE0;&#x5DF2;&#x7ECF;&#x8BB2;&#x8FC7;&#x3002;
&#x5982;&#x679C;&#x8FD9;&#x91CC;&#x628A; initPrice &#x6362;&#x6210; final &#x4FEE;&#x9970;&#x7684;&#xFF0C;&#x4F1A;&#x8F93;&#x51FA;&#x4EC0;&#x4E48;&#x7ED3;&#x679C;&#x5462;&#xFF1F;
&#x7B54;&#x6848;&#x662F; 17.2 &#x3002;
&#x539F;&#x56E0;&#x5C31;&#x662F;&#x8FD9;&#x91CC;&#x6267;&#x884C;&#x4E86;&#x66FF;&#x6362;&#x3002;&#x628A; initPrice &#x76F4;&#x63A5;&#x66FF;&#x6362;&#x6210;&#x4E86; 20&#xFF0C;&#x5C31;&#x4E0D;&#x4F1A;&#x6709;&#x521D;&#x59CB;&#x5316;&#x987A;&#x5E8F;&#x5F15;&#x8D77;&#x7684;&#x95EE;&#x9898;&#x4E86;&#x3002;
&#x4E0B;&#x9762;&#x901A;&#x8FC7;&#x53CD;&#x5C04;&#x6765;&#x9A8C;&#x8BC1;&#x4E00;&#x4E0B;&#xFF1A;</p>
<pre><code>public class TestA {
    private final String mTestFinal = &quot;testFinal&quot;;

    public String getFinalString() {
        return mTestFinal;
    }
}
</code></pre><p>&#x4E0B;&#x9762;&#x662F;&#x6D4B;&#x8BD5;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code>        TestA a = new TestA();
        try {
            Field field = TestA.class.getDeclaredField(&quot;mTestFinal&quot;);
            field.setAccessible(true);
            Object str = field.get(a);
            Log.e(&quot;Test&quot;,&quot;mTestFinal = &quot;+str);
            Method method = TestA.class.getDeclaredMethod(&quot;getFinalString&quot;);
            Object ret = method.invoke(a);
            Log.e(&quot;Test&quot;,&quot;ret = &quot;+ret);
            field.set(a, &quot;test&quot;);
            str = field.get(a);
            Log.e(&quot;Test&quot;,&quot;mTestFinal = &quot;+str);
            ret = method.invoke(a);
            Log.e(&quot;Test&quot;,&quot;ret = &quot;+ret);
        } catch (Exception e) {
            e.printStackTrace();
        }
</code></pre><p>&#x8F93;&#x51FA;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code>Test    : mTestFinal = testFinal
Test    : ret = testFinal
Test    : mTestFinal = test
Test    : ret = testFinal
</code></pre><p>&#x53EF;&#x89C1;&#x4FEE;&#x6539; mTestFinal &#x53D8;&#x91CF;&#x7684;&#x503C;&#xFF0C;&#x5E76;&#x6CA1;&#x6709;&#x6539;&#x53D8; getFinalString &#x65B9;&#x6CD5;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x3002;
&#x539F;&#x56E0;&#x5C31;&#x662F;&#x7531;&#x4E8E; mTestFinal &#x662F;final&#x53D8;&#x91CF;&#xFF0C;getFinalString &#x5DF2;&#x7ECF;&#x88AB;&#x4F18;&#x5316;&#x6210;&#xFF1A;</p>
<pre><code>    public String getFinalString() {
        return &quot;testFinal&quot;;
    }
</code></pre><p>&#x5982;&#x679C;&#x628A; final &#x4FEE;&#x9970;&#x7B26;&#x53BB;&#x6389;&#xFF0C;&#x7ED3;&#x679C;&#x4E3A;&#xFF1A;</p>
<pre><code>Test    : mTestFinal = testFinal
Test    : ret = testFinal
Test    : mTestFinal = test
Test    : ret = test
</code></pre><p>&#x4E0B;&#x9762;&#x518D;&#x4ECB;&#x7ECD;&#x4E00;&#x4E0B;&#x5176;&#x4ED6;&#x60C5;&#x5F62;&#x7684;&#x5B8F;&#x66FF;&#x6362;&#x60C5;&#x51B5;&#xFF1A;</p>
<pre><code>    // &#x4F1A;&#x66FF;&#x6362;&#xFF0C;&#x7F16;&#x8BD1;&#x5668;&#x4F1A;&#x4F18;&#x5316;&#x6210; &quot;testFinalt&quot;
    private final String mTestFinal = &quot;testFinal&quot;+&quot;t&quot;;

    // &#x4F1A;&#x66FF;&#x6362;&#xFF0C;&#x57FA;&#x672C;&#x7684;&#x7B97;&#x672F;&#x8FD0;&#x7B97;
    private final int mTestFinal = 8+1;

    // &#x4E0D;&#x4F1A;&#x66FF;&#x6362;
    private final String mTestFinal = new String(&quot;testFinal&quot;);

    // &#x4E0D;&#x4F1A;&#x66FF;&#x6362;&#xFF0C;&#x8C03;&#x7528;&#x4E86;valueOf&#x65B9;&#x6CD5;&#xFF0C;&#x65E0;&#x6CD5;&#x5728;&#x7F16;&#x8BD1;&#x65F6;&#x786E;&#x5B9A;
    private final String mTestFinal = String.valueOf(&quot;testFinal&quot;);

    // &#x4E0D;&#x4F1A;&#x66FF;&#x6362;&#xFF0C;&#x56E0;&#x4E3A;&#x6CA1;&#x6709;&#x5728;&#x5B9A;&#x4E49;&#x65F6;&#x521D;&#x59CB;&#x5316;&#xFF0C;
    // &#x540C;&#x6837;&#x5728;&#x6784;&#x9020;&#x65B9;&#x6CD5;&#x4E2D;&#x521D;&#x59CB;&#x5316;&#x4E5F;&#x662F;&#x4E0D;&#x4F1A;&#x66FF;&#x6362;&#x7684;
    private final int mTestFinal;
    {
        mTestFinal = 9;
    }
</code></pre><p>&#x6211;&#x4EEC;&#x5728;<a href="http://www.heqiangfly.com/2014/01/20/java-basic-knowledge-about-string/" target="_blank">Java &#x57FA;&#x7840; -- &#x6DF1;&#x5165;&#x7406;&#x89E3; String </a>&#x4E00;&#x6587;&#x4E2D;&#x4ECB;&#x7ECD;&#x8FC7;&#x4E0B;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#xFF1A;</p>
<pre><code>String str1 = &quot;abcdef&quot;;
String abc = &quot;abc&quot;;
String def = &quot;def&quot;;
String str2 = abc + def;
System.out.println(str1 == str2);
</code></pre><p>&#x8FD9;&#x4E2A;&#x6253;&#x5370;&#x8F93;&#x51FA;&#x4E3A;false&#xFF0C;&#x5982;&#x4F55;&#x624D;&#x80FD;&#x8F93;&#x51FA;true&#x5462;&#xFF1F;</p>
<p>&#x628A;abc&#x548C;def&#x6539;&#x6210;final&#x7C7B;&#x578B;&#x5C31;&#x884C;&#x4E86;&#xFF0C;&#x4F1A;&#x6267;&#x884C;&#x5B8F;&#x66FF;&#x6362;&#xFF0C;str2 &#x4ECD;&#x7136;&#x76F8;&#x5F53;&#x4E8E;&#x6267;&#x884C; <code>&quot;abc&quot;+&quot;def&quot;</code>&#x3002;</p>
<h2 id="&#x5185;&#x90E8;&#x7C7B;&#x4E2D;&#x7684;&#x5C40;&#x90E8;&#x53D8;&#x91CF;">&#x5185;&#x90E8;&#x7C7B;&#x4E2D;&#x7684;&#x5C40;&#x90E8;&#x53D8;&#x91CF;</h2>
<p>&#x6211;&#x4EEC;&#x77E5;&#x9053;&#xFF0C;&#x5982;&#x679C;&#x7A0B;&#x5E8F;&#x9700;&#x8981;&#x5728;&#x533F;&#x540D;&#x5185;&#x90E8;&#x7C7B;&#x548C;&#x5C40;&#x90E8;&#x5185;&#x90E8;&#x7C7B;&#x4E2D;&#x4F7F;&#x7528;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#xFF0C;&#x90A3;&#x4E48;&#x8FD9;&#x4E2A;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x5FC5;&#x987B;&#x4F7F;&#x7528;final&#x4FEE;&#x9970;&#x3002;
&#x90A3;&#x4E48;&#x539F;&#x56E0;&#x662F;&#x4EC0;&#x4E48;&#x5462;&#xFF1F;
&#x5BF9;&#x4E8E;&#x666E;&#x901A;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x800C;&#x8A00;&#xFF0C;&#x5B83;&#x7684;&#x4F5C;&#x7528;&#x57DF;&#x5C31;&#x662F;&#x505C;&#x7559;&#x5728;&#x8BE5;&#x65B9;&#x6CD5;&#x5185;&#xFF0C;&#x5F53;&#x65B9;&#x6CD5;&#x6267;&#x884C;&#x7ED3;&#x675F;&#xFF0C;&#x8BE5;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x4E5F;&#x968F;&#x4E4B;&#x6D88;&#x5931;&#xFF1B;&#x4F46;&#x662F;&#x5185;&#x90E8;&#x7C7B;&#x5219;&#x53EF;&#x80FD;&#x4EA7;&#x751F;&#x9690;&#x5F0F;&#x7684;&#x95ED;&#x5305;&#xFF0C;&#x95ED;&#x5305;&#x4F7F;&#x5F97;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x8131;&#x79BB;&#x5B83;&#x6240;&#x5728;&#x7684;&#x65B9;&#x6CD5;&#x7EE7;&#x7EED;&#x5B58;&#x5728;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x5185;&#x90E8;&#x7C7B;&#x4F1A;&#x6269;&#x5927;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x7684;&#x4F5C;&#x7528;&#x57DF;&#x3002;&#x6BD4;&#x5982;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x5C40;&#x90E8; Thread &#x53D8;&#x91CF;&#x91CC;&#x9762;&#x4F7F;&#x7528;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x3002;&#x5982;&#x679C;&#x4E0D;&#x88AB;final&#x4FEE;&#x9970;&#x7684;&#x8BDD;&#xFF0C;&#x8BE5;&#x53D8;&#x91CF;&#x7684;&#x503C;&#x53EF;&#x80FD;&#x4F1A;&#x88AB;&#x968F;&#x610F;&#x7684;&#x6539;&#x53D8;&#xFF0C;&#x90A3;&#x4E48;&#x5C31;&#x4F1A;&#x5F15;&#x8D77;&#x6DF7;&#x4E71;&#xFF0C;&#x56E0;&#x6B64;Java&#x7F16;&#x8BD1;&#x5668;&#x8981;&#x6C42;&#x88AB;&#x5185;&#x90E8;&#x7C7B;&#x8BBF;&#x95EE;&#x7684;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x5FC5;&#x987B;&#x4F7F;&#x7528;final&#x4FEE;&#x9970;&#x3002;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x88AB;final&#x4FEE;&#x9970;&#x4E4B;&#x540E;&#xFF0C;&#x6B64;&#x65F6;&#x4F1A;&#x5728;&#x5185;&#x5B58;&#x4E2D;&#x4FDD;&#x6709;&#x4E00;&#x4EFD;&#x5C40;&#x90E8;&#x53D8;&#x5F97;&#x7684;&#x590D;&#x5236;&#x54C1;&#xFF0C;&#x5F53;&#x5185;&#x90E8;&#x7C7B;&#x8BBF;&#x95EE;&#x7684;&#x65F6;&#x5019;&#x5176;&#x5B9E;&#x8BBF;&#x95EE;&#x7684;&#x662F;&#x8FD9;&#x4E2A;&#x590D;&#x5236;&#x54C1;&#x3002;&#x8FD9;&#x5C31;&#x597D;&#x50CF;&#x662F;&#x628A;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x7684;&#x751F;&#x547D;&#x5468;&#x671F;&#x53D8;&#x957F;&#x4E86;&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="java-basic-io-stream.html" class="navigation navigation-prev " aria-label="Previous page: Java IO -- IO流的概念和分类">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="java-basic-memory-manage-constructor.html" class="navigation navigation-next " aria-label="Next page: Java 内存管理 -- 父子实例的内存控制">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"Java 基础 -- 深入理解 final 修饰符","categories":"Java","comments":true,"tags":["Java"],"description":"介绍 final 修饰符","date":"2014-02-24T10:00:00.000Z","level":"1.1.10","depth":2,"next":{"title":"Java 内存管理 -- 父子实例的内存控制","level":"1.1.11","depth":2,"path":"source/Java/java-basic-memory-manage-constructor.md","ref":"source/Java/java-basic-memory-manage-constructor.md","articles":[]},"previous":{"title":"Java IO -- IO流的概念和分类","level":"1.1.9","depth":2,"path":"source/Java/java-basic-io-stream.md","ref":"source/Java/java-basic-io-stream.md","articles":[]},"dir":"ltr"},"config":{"plugins":["expandable-chapters","livereload"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"expandable-chapters":{},"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"寒江蓑笠的博客","gitbook":"*"},"file":{"path":"source/Java/java-basic-final.md","mtime":"2022-05-17T10:49:59.383Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2022-05-17T10:54:58.084Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

